package app.fugema.shop.main.goods.view;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.LogUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyGridLayoutManager;
import com.base.common.view.adapter.ada.GeneralRecyclerAdapter;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseActivity;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityMiniShopBinding;
import app.fugema.shop.databinding.FragmentMiniShopAllGoodsBinding;
import app.fugema.shop.main.goods.adapter.ShopBannerItemType;
import app.fugema.shop.main.goods.adapter.ShopGoodsBaseItemMultiType;
import app.fugema.shop.main.goods.adapter.ShopHeaderInfoItemMultiType;
import app.fugema.shop.main.goods.adapter.ShopInfoBaseItemMultiType;
import app.fugema.shop.main.goods.model.CateGoodsPageBean;
import app.fugema.shop.main.goods.model.GoodsSearchParams;
import app.fugema.shop.main.goods.model.ShopBannerData;
import app.fugema.shop.main.goods.model.ShopCouponResult;
import app.fugema.shop.main.goods.model.ShopHeadInfoBean;
import app.fugema.shop.main.goods.model.VShopResult;
import app.fugema.shop.main.goods.vm.GoodsVM;
import app.fugema.shop.main.home.adapter.CateGoodsItemBaseItemMultiType;

public class ShopAllGoodsFragment extends BaseFragment<FragmentMiniShopAllGoodsBinding, GoodsVM> implements OnGetDataListener
{

    //微店id
    private int shopId;
    private GeneralRecyclerAdapter adapter;

    private VShopResult shopResult;

    private LinearLayoutManager mGridLayoutMgr;
    private String userKey = "";
    private ShopHeadInfoBean shopHeadInfo;

    private int orderType = 1; ///排序方式（1：升序，2：降序）
    private int orderkey = 1; //排序项（1：默认，2：销量，3：价格，4：评论数，5：上架时间）

    public static ShopAllGoodsFragment getInstance(int mShopId )
    {
        ShopAllGoodsFragment shopMiniFragment = new ShopAllGoodsFragment();
        Bundle args = new Bundle();
        args.putInt("shopId", mShopId);
        shopMiniFragment.setArguments(args);
        return shopMiniFragment;
    }


    @Override
    protected FragmentMiniShopAllGoodsBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.fragment_mini_shop_all_goods, container, false);
    }



    @Override
    public void initView()
    {
        super.initView();
        setTitle("宝贝");
        shopId = getArguments().getInt("shopId",0);

        adapter = new GeneralRecyclerAdapter() {
            @Override
            public void initMultiItemType() {
                putMultiItemType(new CateGoodsItemBaseItemMultiType());
            }
        };

        mGridLayoutMgr = new MyGridLayoutManager(getActivity(), 2);
        binding.rvRecyclerView.setLayoutManager(mGridLayoutMgr);

        binding.rvRecyclerView.setAdapter(adapter);
        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);

        binding.tvBtnDef.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                orderType = 1;
                orderkey = 1;
                initSortView();
                onGetData(1);
            }
        });

        binding.llSaleGroup.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (orderkey == 2) {
                    orderType = orderType == 1 ? 0 : 1;
                }
                //第一次点击
                else {
                    orderkey = 2;
                    orderType = 2;//默认降序
                }
                initSortView();
                onGetData(1);
            }
        });

        binding.llEvaluateGroup.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (orderkey == 4) {
                    orderType = orderType == 1 ? 0 : 1;
                }
                //第一次点击
                else {
                    orderkey = 4;
                    orderType = 1;//默认升序
                }
                initSortView();
                onGetData(1);
            }
        });

        binding.llPriceGroup.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (orderkey == 3) {
                    orderType = orderType == 1 ? 0 : 1;
                }
                //第一次点击
                else {
                    orderkey = 3;
                    orderType = 1;//默认升序
                }
                initSortView();
                onGetData(1);
            }
        });

    }


    @Override
    public void initData()
    {
        super.initData();
        if( LoginUtils.isLogin() )
        {
            userKey = LoginUtils.getToken();
        }

        onGetData(1);


        //getShopData();

    }


    private void getShopData()
    {
        viewModel.getShopById(shopId).observe(this, new BaseViewObserver<LiveDataWrapper<VShopResult>>(viewUtils.getStateLayout())
        {
            @Override
            protected void onSuccess(LiveDataWrapper<VShopResult> data)
            {
                shopResult = data.data;
                shopHeadInfo = new ShopHeadInfoBean();
                shopHeadInfo.setItemType(BaseItemTypeInterface.TYPE_HEAD_ONE);
                shopHeadInfo.setShopBean( shopResult.getVShop() );
                if( UIUtils.isNotEmpty( shopResult.getCoupon() ) )
                {
                    shopHeadInfo.setCoupon(shopResult.getCoupon());
                }
                adapter.addHeaderView(shopHeadInfo, 0);

                //店铺 轮播图
                ShopBannerData bannerData = new ShopBannerData();
                bannerData.setItemType(BaseItemTypeInterface.TYPE_HEAD);
                bannerData.setData( shopResult.getSlideImgs() );
                adapter.addHeaderView(bannerData, 1);
                //店铺信息
                shopResult.getVShop().setItemType(BaseItemTypeInterface.TYPE_HEAD_TWO);
                adapter.addHeaderView(shopResult.getVShop(), 2);
                // 店铺 推荐商品
                adapter.setDataList( data.data.getProducts() );



            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<VShopResult> data)
            {
                // || (data.data.getData().getCurrent() == 1 && UIUtils.isEmpty(data.data.getData().getRecords())
                return UIUtils.isEmpty(data.data);
            }
        });



    }


    private void queryShopCoupon()
    {
        //店铺优惠券信息
        viewModel.getShopCouponListByShopId( shopResult.getVShop().getShopId() ,userKey )
                .observe(getActivity(), new BaseViewObserver<LiveDataWrapper<ShopCouponResult>>() {
                    @Override
                    protected void onSuccess(LiveDataWrapper<ShopCouponResult> data)
                    {
                        LogUtil.e( "店铺优惠券信息" );
                        //{"success":false,"msg":"该店铺没有可领优惠券","code":0}

                        if( data.data.isSuccess() )
                        {
                            if( !data.data.getCoupon().isEmpty() )
                            {
                                int couponId = data.data.getCoupon().get(0).getCouponId();
                                LogUtil.e( "开始领取优惠券" );
                                        /*
                                        viewModel.applyCoupon(shopResult.getVShop().getShopId(),couponId ,userKey  )
                                                 .observe(ShopActivity.this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                                                 {
                                                     @Override
                                                     protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                                     {
                                                         //{"success":true,"msg":"", "code":1}
                                                         if( data.data.isSuccess() )
                                                         {
                                                             ToastUtils.showShortToast(ShopActivity.this, "领取成功" );
                                                         }
                                                         else
                                                         {
                                                             ToastUtils.showShortToast(ShopActivity.this, data.data.getMsg() );
                                                         }
                                                     }
                                                 });
                                        */

                                ((BaseActivity)getActivity()).addFragment(  ShopCouponDialogFragment.getInstance(data.data.getCoupon(),0  ,null) );
                            }
                        }
                    }
                });
    }


    private void initSortView() {

        switch (orderkey) {
            case 1://默认的
                orderType = 1;//默认升序
                binding.tvBtnDef.setSelected(true);
                binding.tvBtnSale.setSelected(false);
                binding.tvEvaluate.setSelected(false);
                binding.tvBtnPrice.setSelected(false);


                binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleTop.setRotation(0);
                binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleDown.setRotation(180);

                binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateTop.setRotation(0);
                binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateDown.setRotation(180);

                binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceTop.setRotation(0);
                binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceDown.setRotation(180);

                break;
            case 2:// 销量

//                orderType = 2;//默认降序
                binding.tvBtnDef.setSelected(false);
                binding.tvBtnSale.setSelected(true);
                binding.tvEvaluate.setSelected(false);
                binding.tvBtnPrice.setSelected(false);


                // 排序(1升序 2降序)
                if (orderType == 1) {
                    binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivSaleTop.setRotation(180);
                    binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivSaleDown.setRotation(180);
                } else {
                    binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivSaleTop.setRotation(0);
                    binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivSaleDown.setRotation(0);
                }


                binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateTop.setRotation(0);
                binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateDown.setRotation(180);

                binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceTop.setRotation(0);
                binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceDown.setRotation(180);

                break;
            case 4://评价
//                orderType = 1;//默认升序
                binding.tvBtnDef.setSelected(false);
                binding.tvBtnSale.setSelected(false);
                binding.tvEvaluate.setSelected(true);
                binding.tvBtnPrice.setSelected(false);


                binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleTop.setRotation(0);
                binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleDown.setRotation(180);

                // 排序(1升序 2降序)
                if (orderType == 1) {
                    binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivEvaluateTop.setRotation(180);
                    binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivEvaluateDown.setRotation(180);
                } else {
                    binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivEvaluateTop.setRotation(0);
                    binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivEvaluateDown.setRotation(0);
                }

                binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceTop.setRotation(0);
                binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivPriceDown.setRotation(180);


                break;
            case 3://价格
//                orderType = 1;//默认升序
                binding.tvBtnDef.setSelected(false);
                binding.tvBtnSale.setSelected(false);
                binding.tvEvaluate.setSelected(false);
                binding.tvBtnPrice.setSelected(true);


                binding.ivSaleTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleTop.setRotation(0);
                binding.ivSaleDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivSaleDown.setRotation(180);

                binding.ivEvaluateTop.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateTop.setRotation(0);
                binding.ivEvaluateDown.setImageResource(R.mipmap.ic_triangle_top);
                binding.ivEvaluateDown.setRotation(180);


                // 排序(1升序 2降序)
                if (orderType == 1) {
                    binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivPriceTop.setRotation(180);
                    binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivPriceDown.setRotation(180);
                } else {
                    binding.ivPriceTop.setImageResource(R.mipmap.ic_triangle_top);
                    binding.ivPriceTop.setRotation(0);
                    binding.ivPriceDown.setImageResource(R.mipmap.ic_triangle_down);
                    binding.ivPriceDown.setRotation(0);
                }
                break;
        }
    }


    @Override
    public void onGetData(int pn)
    {

        GoodsSearchParams searchParams=new GoodsSearchParams();
        searchParams.setPageno(pn);
        searchParams.setVshopid(shopId);
        searchParams.setOrderkey(orderkey);
        searchParams.setOrdertype(orderType);
        if (LoginUtils.isLogin()) {
            searchParams.setUserkey(LoginUtils.getToken());
        }
        //viewModel.getPageGoodsByCateId(categoryId, pn)
        viewModel.getPageGoodsByParams(searchParams).observe(this, new BaseViewObserver<LiveDataWrapper<CateGoodsPageBean>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<CateGoodsPageBean> data) {
                if (UIUtils.isNotEmpty(data.data.getProduct())) {
                    for (CateGoodsPageBean.ProductBean tempProduct : data.data.getProduct()) {
                        //tempProduct.setItemType(BaseItemTypeInterface.TYPE_HEAD_ONE);
                    }
                }
                viewUtils.setDataListRefreshLayout(adapter, pn, 10, data.data.getProduct());
            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<CateGoodsPageBean> data) {
                // || (data.data.getData().getCurrent() == 1 && UIUtils.isEmpty(data.data.getData().getRecords())
                return pn == 1 && UIUtils.isEmpty(data.data.getProduct());
            }
        });

    }


}
